package com.bnlr.lrparking.appui.carport;

import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.TextView;

import com.bigkoo.pickerview.OptionsPickerView;
import com.blankj.utilcode.util.ToastUtils;
import com.bnlr.lrparking.R;
import com.bnlr.lrparking.base.BaseActivity;
import com.bnlr.lrparking.base.BaseJavaBean;
import com.bnlr.lrparking.app.AppConfig;
import com.bnlr.lrparking.model.CarportBuildIdentityBean;
import com.bnlr.lrparking.constant.LrConstant;
import com.bnlr.lrparking.constant.Urls;
import com.bnlr.lrparking.appui.carport.bean.CarportBuildResultBean;
import com.bnlr.lrparking.view.ClearEditText;
import com.bnlr.lrparking.widget.boarddialog.KeyBoardDialog;
import com.bnlr.nohttp.CallServer;
import com.bnlr.nohttp.CustomJavaHttpListener;
import com.yolanda.nohttp.NoHttp;

import org.json.JSONException;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import butterknife.Unbinder;

/**
 * 车位投建
 *
 * @author tian
 * @date 2018/3/27
 */
public class CarportBuildActivity extends BaseActivity {

    @BindView(R.id.tv_nav_title)
    TextView tvNavTitle;
    @BindView(R.id.tv_id)
    TextView tvId;
    @BindView(R.id.et_name)
    ClearEditText etName;
    @BindView(R.id.man)
    RadioButton man;
    @BindView(R.id.rb_woman)
    RadioButton rbWoman;
    @BindView(R.id.radioGroup)
    RadioGroup radioGroup;
    @BindView(R.id.tv_phone)
    TextView tvPhone;
    @BindView(R.id.ll_build_apply)
    LinearLayout llBuildApply;
    @BindView(R.id.ll_build_result)
    LinearLayout llBuildResult;
    private Unbinder unbinder;
    private OptionsPickerView optionsPickerView;
    private List<CarportBuildIdentityBean.ModelBean> IdData = new ArrayList<>();
    private int selectId;
    private String username, publishPhone;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_carport_build);
        unbinder = ButterKnife.bind(this);
        TextView tvTitle = (TextView) findViewById(R.id.tv_nav_title);
        tvTitle.setText("车位投建");
        username = AppConfig.getInstance().getString("userName", "");
        publishPhone = username;
        if (!username.equals("")) {
            tvPhone.setText(username.substring(0, 3) + "****" + username.substring(7));
        }
        getCarportBuildResult();
        getCarportBuildIdentity();
    }

    /**
     * 判断是否提交过信息
     */
    private void getCarportBuildResult() {
        try {
            mRequest = NoHttp.createStringRequest(Urls.BASE_JAVA_URL+"system/getInvestList", LrConstant.POST);
            // 添加到请求队列+
            mRequest.add("uid", AppConfig.getInstance().getString("uid", ""));
            CallServer.getRequestInstance().add(this, mRequest,
                    new CustomJavaHttpListener<CarportBuildResultBean>(this, true, CarportBuildResultBean.class) {
                        @Override
                        public void doWork(CarportBuildResultBean data, int code) {
                           if (data.getModel().getData().size() > 0) {
                               llBuildResult.setVisibility(View.VISIBLE);
                           } else {
                               llBuildApply.setVisibility(View.VISIBLE);
                           }
                        }

                        @Override
                        public void onFinally(String obj, int code, boolean isSucceed) throws JSONException {
                            super.onFinally(obj, code, isSucceed);
                            if (code != 0) {
                                llBuildApply.setVisibility(View.VISIBLE);
                            }
                        }
                    }, true);
        } catch (Exception e) {
        }
    }

    /**
     * 获取车位投建人身份
     */
    private void getCarportBuildIdentity() {
        try {
            mRequest = NoHttp.createStringRequest(Urls.BASE_JAVA_URL+"system/getIdentityList", LrConstant.POST);
            // 添加到请求队列+
            CallServer.getRequestInstance().add(this, mRequest,
                    new CustomJavaHttpListener<CarportBuildIdentityBean>(this, true, CarportBuildIdentityBean.class) {
                        @Override
                        public void doWork(CarportBuildIdentityBean data, int code) {
                            IdData = data.getModel();
                            initPicker();
                        }

                        @Override
                        public void onFinally(String obj, int code, boolean isSucceed) throws JSONException {
                            super.onFinally(obj, code, isSucceed);

                        }
                    }, false);
        } catch (Exception e) {
        }
    }

    /**
     * 初始化身份弹窗
     */
    private void initPicker() {
        optionsPickerView = new OptionsPickerView.Builder(this, new OptionsPickerView.OnOptionsSelectListener() {
            @Override
            public void onOptionsSelect(int options1, int options2, int options3, View v) {
                selectId = IdData.get(options1).getValue();
                tvId.setText(IdData.get(options1).getTxt());
            }
        }).build();
        //  添加数据
        optionsPickerView.setPicker(IdData);
    }

    @OnClick({R.id.tv_id, R.id.tv_car_build_submit,R.id.tv_phone})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.tv_id:
                if (optionsPickerView != null) {
                    optionsPickerView.show();
                }
                break;
            case R.id.tv_car_build_submit:
                if (TextUtils.isEmpty(tvId.getText().toString())) {
                    ToastUtils.showShort("请选择身份");
                    return;
                }
                if (TextUtils.isEmpty(etName.getText().toString())) {
                    ToastUtils.showShort("请填写联系人");
                    return;
                }
                submitCarBuildApply();
                break;
            case R.id.tv_phone:
                KeyBoardDialog dialog = new KeyBoardDialog(this, publishPhone);
                dialog.setOnDialogResultListener(new KeyBoardDialog.OnDialogResultListener() {
                    @Override
                    public void onResult(String result) {
                        if (result.equals("some")) {
                            ToastUtils.showShort("此手机号无需再次验证");
                        } else if (!result.equals("fail")) {
                            publishPhone = result;
                            tvPhone.setText(publishPhone.substring(0, 3) + "****" + publishPhone.substring(7));
                        }
                    }
                });
                dialog.show();
                break;
        }
    }

    /**
     * 提交车位投建申请信息
     */
    private void submitCarBuildApply() {
        try {
            mRequest = NoHttp.createStringRequest(Urls.BASE_JAVA_URL+"system/saveInvest", LrConstant.POST);
            mRequest.add("uid", AppConfig.getInstance().getString("uid", ""));
            mRequest.add("identity", selectId);
            mRequest.add("name", etName.getText().toString());
            if (radioGroup.getCheckedRadioButtonId() == R.id.man) {
                mRequest.add("sex", "先生");
            } else {
                mRequest.add("sex", "女士");
            }
            mRequest.add("phone", publishPhone);
            // 添加到请求队列+
            CallServer.getRequestInstance().add(this, mRequest,
                    new CustomJavaHttpListener<BaseJavaBean>(this, true, BaseJavaBean.class) {
                        @Override
                        public void doWork(BaseJavaBean data, int code) {
                            startActivity(new Intent(CarportBuildActivity.this,CarportResultActivity.class)
                            .putExtra("carport_tag",0));
                            finish();
                        }

                        @Override
                        public void onFinally(String obj, int code, boolean isSucceed) throws JSONException {
                            if (code != 0) {
                                ToastUtils.showShort(obj+"");
                            }

                        }
                    }, false);
        } catch (Exception e) {
        }
    }

    @Override
    protected void onDestroy() {
        if (unbinder != null) {
            unbinder.unbind();
        }
        super.onDestroy();
    }
}
